package com.huangliusong.threadcurrency.example.sync;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author huangliusong
 * @since 2018/11/18
 * {@link }
 */
@Slf4j
public class SynchronizedExample1 {


    //修饰一个代码块
    public void test1(){
        synchronized (this){
            for (int i=0;i<10;i++){
                log.info("test1:{}",i);
            }
        }
    }

    public synchronized void test2(){
        for (int i=0;i<10;i++){
            log.info("#########################test2:{}",i);
        }
    }

    public static void main(String[] args) {
        SynchronizedExample1 synchronizedExample1=new SynchronizedExample1();
        ExecutorService executorService=Executors.newCachedThreadPool();
        executorService.execute(()->{
            synchronizedExample1.test2();
        });
        executorService.execute(()->{
            synchronizedExample1.test2();
        });

        executorService.shutdown();


;    }
}
